<template>
    <div class="page-box">
        <div class="page-search">

            <div>
                <label>单据查询</label>
                <el-input v-model="searchForm.Number" style="width: 160px;"></el-input>
            </div>
            <div>
                <label>客户名称</label>
                <el-select v-model="searchForm.CustomerNumber" style="width: 160px;" placeholder="请选择客户"
                    @change="CustomerChange" allow-create filterable>
                    <el-option v-for="Client in ClientList" :key="Client.CustomerNumber" :label="Client.ClientName"
                        :value="Client.Id"></el-option>
                </el-select>
            </div>

            <div>
                <label>报价人</label>
                <el-select v-model="searchForm.EmployeesId" style="width: 160px;" placeholder="请选择报价人" 
                   @change="EmployeesChange" allow-create filterable>
                    <el-option v-for="Employees in EmployeesList" :key="Employees.Id" :label="Employees.Name"
                        :value="Employees.Id"></el-option>
                </el-select>
            </div>
            <div>
                <label>出库仓</label>
                <el-select v-model="searchForm.WarehouseId" style="width: 160px;" placeholder="请选择出库仓" @change="WarehouseChange"  allow-create
                    filterable>
                    <el-option v-for="Warehouse in WarehouseList" :key="Warehouse.Id" :label="Warehouse.WarehouseName"
                        :value="Warehouse.Id"></el-option>
                </el-select>
            </div>
            <div>
                <label>订单状态</label>
                <el-select v-model="searchForm.Status" style="width: 160px;" placeholder="请选择订单状态" allow-create
                    filterable>
                    <el-option label="草稿" value="0"></el-option>
                    <el-option label="已确认" value="1"></el-option>
                    <el-option label="已作废" value="2"></el-option>

                </el-select>
            </div>

            <div>
                <el-button type="primary" @click="search">查询</el-button>
                <el-button @click="reset">重置</el-button>
            </div>

        </div>
        <div class="page-section">

            <div class="page-tool">
                <el-button type="primary" plain icon="el-icon-plus" @click="add" size="small">新增</el-button>
                <el-button type="danger" plain icon="el-icon-delete" @click="dels" size="small">删除</el-button>
                <el-dropdown class="mg-l">
                    <el-button size="small" type="primary" plain>
                        <i class="el-icon-s-grid"></i>
                        <i class="el-icon-arrow-down el-icon--right"></i>
                    </el-button>
                    <el-dropdown-menu slot="dropdown">
                        <el-dropdown-item @click.native="exports"><i class="el-icon-receiving"></i>导出</el-dropdown-item>
                    </el-dropdown-menu>
                </el-dropdown>
            </div>
            <el-table :data="tableData" ref="dataForm" v-loading="isLoading" height="calc(100vh - 350px)">
                <el-table-column type="selection" width="55"></el-table-column>
                <el-table-column fixed prop="Number" label="报价单编号" :show-overflow-tooltip="true"></el-table-column>
                <el-table-column prop="CustomerName" label="客户名称" :show-overflow-tooltip="true"></el-table-column>
                <el-table-column prop="WarehouseName" label="出货仓" :show-overflow-tooltip="true"></el-table-column>
                <el-table-column prop="Transfer" label="运输方式" :show-overflow-tooltip="true"></el-table-column>
                <el-table-column prop="EmployeesName" label="报价人" :show-overflow-tooltip="true"></el-table-column>
                <el-table-column prop="Status" label="报价单状态" :show-overflow-tooltip="true">
                    <template slot-scope="scope">
                        <el-tag type="info" v-if="scope.row.Status === 0">草稿</el-tag>
                        <el-tag type="success"  v-else-if="scope.row.Status === 1">已确认</el-tag>
                        <el-tag type="danger" v-else-if="scope.row.Status === 2">已作废</el-tag>
                    </template>
                </el-table-column>

                <el-table-column prop="CreateTime" label="出库日期" :show-overflow-tooltip="true"></el-table-column>
                <el-table-column prop="Remark" label="备注" :show-overflow-tooltip="true"></el-table-column>

                <el-table-column label="操作" :show-overflow-tooltip="true" width="220">
                    <template slot-scope="scope">
                        <el-button type="text" @click="update(scope.row)" :disabled="scope.row.Status === 1">编辑</el-button>
                        <el-button  type="text" @click="confirmQuote(scope.row)" :disabled="scope.row.Status === 1">确认报价</el-button>
                        <el-button  type="text" @click="cancelQuote(scope.row)" :disabled="scope.row.Status === 1">作废</el-button>
                        <el-button  type="text" @click="detailsQuote(scope.row)">明细</el-button>
                    </template>
                </el-table-column>


            </el-table>

            <Pagination @handleSizeChange="handleSizeChange" @handleCurrentChange="handleCurrentChange"
                        :PageIndex="pageDomain.PageIndex" :PageSize="pageDomain.PageSize"
                        :TotalCount="pageDomain.TotalCount" />

        </div>

        <el-dialog :close-on-click-modal="false" :title="dialogDomain.title" :visible.sync="dialogDomain.dialogVisible"
            width="65%" @close="closeDialog">
            <OrderQuoteInput :ClientList="ClientList" :EmployeesList="EmployeesList" :WarehouseList="WarehouseList" :model="model" :isUpdate="isUpdate"
                @callback="callback" v-if="dialogDomain.dialogVisible && dialogDomain.dialogType==0 " />
                <OrderQuoteDetails  v-else-if="dialogDomain.dialogVisible && dialogDomain.dialogType==1 "  :model="model"/>
        </el-dialog>

    </div>
</template>



<script>
import OrderQuoteInput from './components/OrderQuoteInput.vue'
import OrderQuoteDetails from './components/OrderQuoteDetails.vue'
import Pagination from '@/components/Pagination.vue';

export default {
    components: {
        Pagination,
        OrderQuoteInput,
        OrderQuoteDetails
    },
    mounted() {
        this.dataBind();

    },
    data() {
        return {
            ClientList: [],
            EmployeesList: [],
            WarehouseList: [],
            searchForm: {
                Number: '',
                CustomerName: '',
                EmployeesId: '',
                WarehouseId: '',
                Status: ''
            },
            dialogDomain: {
                title: '新增客户报价单',
                dialogVisible: false,
                dialogType: 0

            },
            isUpdate: false,
            //数据模型
            model: {
                Number: '',
                CustomerNumber: '',
                CustomerName: '',

                WarehouseId: '',
                WarehouseName: '',
                Transfer: '',
                EmployeesId: '',
                EmployeesName: '',
                Name: '',

                Status: '0',
                CreateTime: '',
                Remark: '',


            },
            pageDomain: {
                PageIndex: 1,
                PageSize: 10,
                TotalCount: 0
            },
            tableData: [],
            isLoading: false
        }
    },
    created() {
        this.GetOrderQuoteSelectData();
    },
    methods: {
        detailsQuote(model){
            this.dialogDomain.title = "报价单明细";
            this.dialogDomain.dialogVisible = true;
            this.dialogDomain.dialogType = 1;
            this.model = model;
        },

        CustomerChange(model) {
            this.model.CustomerNumber = model.Id;
            this.model.CustomerName = model.ClientName;
            console.log(model)
        },
        EmployeesChange(model) {
            this.model.EmployeesId = model.Id;
            this.model.EmployeesName = model.Name;
            console.log(model)
        },
        WarehouseChange(model) {
            this.model.WarehouseId = model.Id;
            this.model.WarehouseName = model.WarehouseName;
            console.log(model)
        },
        add() {
            this.dialogDomain.title = "新增报价单"
            this.dialogDomain.dialogVisible = true;
            this.dialogDomain.dialogType=0;
            this.isUpdate = false;


        },
        update(model) {
            this.dialogDomain.title = "编辑报价单";
            this.dialogDomain.dialogVisible = true;
            this.isUpdate = true;
            this.dialogDomain.dialogType = 0;
            console.log(model);
            this.model = model;

            
        },

        confirmQuote(model) {
            this.$confirm('确定更改选中的数据吗？ 是否继续?', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            })
                .then(() => {
                    this.$http.putForm('/api/OrderQuote/updateQuoteStatus', {
                            id: model.Id,
                            Status: "1" // 将状态设置为确认
                        })
                        .then((res) => {
                            this.showNotify('操作成功', 'success');
                            this.dataBind();
                        })
                }).catch(() => { });
        },

        cancelQuote(model) {
            this.$confirm('确定更改选中的数据吗？ 是否继续?', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            })
                .then(() => {
                    this.$http.putForm('/api/OrderQuote/updateQuoteStatus', {
                        id: model.Id,
                        Status: "2" // 将状态设置为作废
                    })
                        .then((res) => {
                            this.showNotify('操作成功', 'success');
                            this.dataBind();
                        })
                }).catch(() => { });

        },


        //批量删除
        dels() {
            if (this.$refs.dataForm.selection.length <= 0) {
                this.showNotify('请先选择一项')
                return;
            }
            this.$confirm('确定删除选中的数据及其子项吗？这将不可恢复, 是否继续?', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            })
                .then(() => {

                    //获取选中的表格数据 并筛选id
                    var ids = this.$refs.dataForm.selection.map(x => x.Id)

                    //拼接参数
                    let params = new URLSearchParams();
                    params.append('ids', ids.join(','))

                    this.$http.delete('/api/OrderQuote/DelAll', {
                        data: params,
                        headers: {
                            'content-Type': 'application/x-www-form-urlencoded'
                        },
                        loading: this.$loading()
                    })
                        .then((res) => {
                            this.showNotify('操作成功', 'success'); // 删除成功的提示
                            this.dataBind(); // 重新绑定数据
                        })
                        .catch((error) => {
                            console.error('删除请求出错:', error);
                            this.showNotify('删除请求出错', 'error');
                        });
                }).catch(() => { });

        },

        //对话框关闭事件
        closeDialog() {
            this.model = {
                Number: '',
                CustomerNumber: '',
                CustomerName: '',

                WarehouseId: '',
                WarehouseName: '',
                Transfer: '',
                EmployeesId: '',
                EmployeesName:'',
                Name: '',

                Status: '0',
                CreateTime: '',
                Remark: '',

            }
        },

        search() {
            this.pageDomain.PageIndex = 1;
            this.dataBind();
        },
        reset() {
            this.searchForm.Number = '';
            this.searchForm.CustomerName = '';
            this.searchForm.EmployeesId = '';
            this.searchForm.WarehouseId = '';
            this.searchForm.Status = '';
        },
        //新增或编辑页面回调
        callback() {
            let actionName = this.isUpdate ? '编辑' : '新增';
            this.showNotify(actionName + '成功', 'success');
            this.dialogDomain.dialogVisible = false;
            this.dataBind();
        },

        dataBind() {
            this.isLoading = true;
            this.$http.get('/api/OrderQuote',
                {
                    params: {
                        PageIndex: this.pageDomain.PageIndex,
                        PageSize: this.pageDomain.PageSize,
                        Number: this.searchForm.Number,
                        CustomerNumber: this.searchForm.CustomerNumber,
                        EmployeesId: this.searchForm.EmployeesId,
                        WarehouseId: this.searchForm.WarehouseId,
                        Status: this.searchForm.Status,
                    }
                })
                .then(res => {
                    this.tableData = res.Data;
                    this.pageDomain.TotalCount = res.Count;
                    this.isLoading = false;
                })
        },
        GetOrderQuoteSelectData() {
            this.$http.get('./api/Integrated/GetOrderQuoteSelectData')
                .then(res => {
                    console.log(res.Data);
                    this.ClientList = res.Data.ClientList;
                    this.EmployeesList = res.Data.EmployeesList;
                    this.WarehouseList = res.Data.WarehouseList;

                })

        },
        
        //分页方法
        handleSizeChange(val) {
            this.pageDomain.PageIndex = 1;
            this.pageDomain.PageSize = val;
            this.dataBind();
        },
        handleCurrentChange(val) {
            this.pageDomain.PageIndex = val;
            this.dataBind();
        },

        exports() {
            this.$confirm('即将导出报表，是否继续？', '系统提示', {
                confirmButtonText: '导出',
                cancelButtonText: '取消',
                type: 'warning'
            }).then(() => {
                this.$http.request({
                    method: "GET",
                    loading: this.$loading(),
                    url: "/api/OrderQuote/Export",
                    responseType: 'blob'
                }).then((res) => {
                    const blob = new Blob([res])
                    const downloadElement = document.createElement('a');
                    const href = window.URL.createObjectURL(blob); //创建下载的链接
                    downloadElement.href = href;
                    downloadElement.download = '销售报价单信息.xls'; //下载后文件名
                    downloadElement.click(); //点击下载
                    window.URL.revokeObjectURL(href); //释放掉blob对象
                })


            }).catch((err) => {
                console.log(err);
            })

        },


    }

}
</script>

<style></style>